package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;
import com.alibaba.fastjson.JSONArray;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.plaf.synth.SynthSpinnerUI;

@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("struts-default")
@Actions
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{

    @Autowired
    private StandardService standardService;
    public  int rows;//属性驱动要使用public
    public int page;


    private Standard standard = new Standard();
    public Standard getModel() {
        return standard;
    }

    @Action(value = "standard_save",
    results={@Result(name = "success",location ="/pages/base/standard.html" ,type = "redirect")})
    public String save(){
        standardService.save(standard);
        return SUCCESS;
    }
    //分页查询
    @Action(value = "standard_pageQuery")
    public String pageQuery() throws IOException {
        Pageable pageable = new PageRequest(page -1,rows);

        Page<Standard> pageData =  standardService.pageQuery(pageable);
        Map<String,Object> result = new HashMap();
        result.put("total",pageData.getTotalElements());
        result.put("rows",pageData.getContent());
        String json = JSONArray.toJSONString(result);
        ServletActionContext.getResponse().getWriter().write(json);
        return  null;

    }
    //查询所有
    @Action(value = "standard_queryAll")
    public String queryAll() throws IOException {
        List<Standard> standarads = standardService.queryAll();
       String json =  JSONArray.toJSONString(standarads);
       ServletActionContext.getResponse().getWriter().write(json);
      System.out.print(json);
        return null;
    }
}
